package com.smart.list.dao.impl;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Date;
import java.util.List;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.smart.list.dao.TaskDao;
import com.smart.list.entity.Context;
import com.smart.list.entity.Task;

@Repository("TaskDao")
public class TaskDaoSOLRimpl implements TaskDao {
	private static final Logger logger = LoggerFactory.getLogger(TaskDaoSOLRimpl.class);
	private CommonsHttpSolrServer server;
	
	public TaskDaoSOLRimpl() {
		String url = "http://localhost:8983/solr";
		try {
			  server = new CommonsHttpSolrServer( url );
			  server.setSoTimeout(1000);  // socket read timeout
			  server.setConnectionTimeout(100);
			  server.setDefaultMaxConnectionsPerHost(100);
			  server.setMaxTotalConnections(100);
			  server.setFollowRedirects(false);  // defaults to false
			  // allowCompression defaults to false.
			  // Server side must support gzip or deflate for this to have any effect.
			  server.setAllowCompression(true);
			  server.setMaxRetries(1); // defaults to 0.  > 1 not recommended.
			  server.setParser(new XMLResponseParser()); // binary parser is used by default
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean createTask(Task task) {
		SolrInputDocument doc = new SolrInputDocument();
		doc.addField("task", task.getTask());
		UpdateResponse response;
		
		try {
			server.add(doc);
			response = server.commit();
			logger.info("doc sent to solr: " + response.getStatus() );
		} catch (SolrServerException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public Task readTask(long id) {
		QueryResponse rsp = null;
		SolrQuery query = new SolrQuery();
	    query.setQuery( "*:*" );
	    
	    try {
			rsp = server.query( query );
		} catch (SolrServerException e) {
			if(logger.isInfoEnabled())
				logger.info("Error in reading data from Solr");
			e.printStackTrace();
		}
		SolrDocumentList docs = rsp.getResults();
		SolrDocument solrDoc = docs.get(0);
		Task task = new Task((String) solrDoc.getFieldValue("task"));
		return task;
	}

	@Override
	public boolean updateTask(Task task) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean deleteTask(Task task) {
		// TODO Auto-generated method stub
		return false;
	}
	
	public List<Task> getTasksByConcept(Context context) {

		return null;
	}

}
